package com.jsgt.greekfire.ui.mainassets.coindetail

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseActivity
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.jsgt.greekfire.IS_ACTIVATION
import com.jsgt.greekfire.LOGIN_STATUS
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.MainAssetsBean
import com.jsgt.greekfire.common.util.NetDataManager
import com.jsgt.greekfire.event.AssetsEvent
import com.jsgt.greekfire.ui.home.entrance.EntranceActivity
import com.jsgt.greekfire.ui.mainassets.ConvertActivity
import com.jsgt.greekfire.ui.mainassets.NewTransferActivity
import com.jsgt.greekfire.ui.mainassets.adapter.TabEntity
import com.jsgt.greekfire.ui.mine.coindetail.recharge.RechargeActivity
import com.jsgt.greekfire.ui.news.NewsFragment
import com.jsgt.greekfire.ui.trading.adapter.PagerAdapter
import kotlinx.android.synthetic.main.activity_currency_details.*
import kotlinx.android.synthetic.main.view_title.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.math.BigDecimal

class CurrencyDetailsActivity : BaseActivity(), View.OnClickListener {
    private var assetsBean: MainAssetsBean? = null
    private var mTabEntities =
        ArrayList<CustomTabEntity>()

    private val mFragments: MutableList<BaseFragment> = ArrayList()
    private var coinIntroductionFragment: CoinIntroductionFragment? = null
    private var coinDividendInfoFragment: CoinDividendInfoFragment? = null
    private var coinAPPApplicationFragment: CoinAPPApplicationFragment? = null
    private var coinNewsDynamicFragment: CoinNewsDynamicFragment? = null

    companion object {
        const val ADDRESS_BEAN = "address_bean"
        private const val ASSETS_BEAN = "bean"
        fun start(context: Context, assetsBean: MainAssetsBean) {
            val intent = Intent(context, CurrencyDetailsActivity::class.java)
            intent.putExtra(ASSETS_BEAN, assetsBean)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_currency_details

    override fun main() {
        assetsBean = intent.getParcelableExtra(ASSETS_BEAN)
        if (assetsBean != null) {
            title_tv?.text = assetsBean?.name
        }
        right_iv.setImageResource(R.mipmap.coin_detail)
        right_iv.visibility = View.VISIBLE
        llRecharge.setOnClickListener(this)
        llTransfers.setOnClickListener(this)
        llConvert.setOnClickListener(this)
        right_iv.setOnClickListener(this)
        val tabArray =
            resources.getStringArray(R.array.tab_currency_details_list)
        val tabs = java.util.ArrayList<String>()
        coinIntroductionFragment =
            CoinIntroductionFragment.newInstance(assetsBean!!.assetId!!, assetsBean!!.name!!)
        coinDividendInfoFragment = CoinDividendInfoFragment.newInstance(assetsBean!!.name!!)
        coinAPPApplicationFragment = CoinAPPApplicationFragment.newInstance(assetsBean!!.name!!)
        coinNewsDynamicFragment = CoinNewsDynamicFragment.newInstance(assetsBean!!.name!!)
        mFragments.add(coinIntroductionFragment!!) //简介
        mFragments.add(coinDividendInfoFragment!!) //分红信息
        mFragments.add(coinAPPApplicationFragment!!) //APP应用
//        mFragments.add(coinNewsDynamicFragment!!) //新闻动态
        mFragments.add(NewsFragment()) //新闻动态

        for (i in tabArray.indices) {
            mTabEntities.add(TabEntity(tabArray!![i]))
            tabs.add(tabArray[i])
        }
        viewPager.adapter = PagerAdapter(
            supportFragmentManager,
            mFragments,
            tabs
        )
        tl_2()

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAssetsEvent(e: AssetsEvent?) {
        for (bean in NetDataManager.assetsList) {
            if (bean.assetName.equals(assetsBean?.name)) {
                var totalAssets = bean.assetBalance!! + bean.assetFrozen!!
                tvTotalAssets.setText(
                    BigDecimal(totalAssets.toString()).setScale(
                        8,
                        BigDecimal.ROUND_DOWN
                    ).stripTrailingZeros().toPlainString()
                )
                tvBalance.setText(
                    BigDecimal(bean.assetBalance.toString()).setScale(
                        8,
                        BigDecimal.ROUND_DOWN
                    ).stripTrailingZeros().toPlainString()
                )
                tvFreeze.setText(
                    BigDecimal(bean.assetFrozen.toString()).setScale(
                        8,
                        BigDecimal.ROUND_DOWN
                    ).stripTrailingZeros().toPlainString()
                )

            }
        }
    }

    private fun tl_2() {
        commontab.setTabData(mTabEntities)
        commontab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                viewPager.setCurrentItem(position)
            }

            override fun onTabReselect(position: Int) {}
        })
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                commontab.setCurrentTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        viewPager.setCurrentItem(0)
    }

    override fun onClick(v: View?) {

        when (v?.id) {
            R.id.right_iv -> {
                //资金明细
                if (assetsBean != null && !TextUtils.isEmpty(assetsBean?.name)) {
                    if (assetsBean != null && !TextUtils.isEmpty(assetsBean?.name)) {
                        var intent = Intent(this, CoinNewDetailActivity::class.java)
                        intent.putExtra("assetGuid", assetsBean?.assetGuid!!.toInt())
                        intent.putExtra("symbol", assetsBean?.name!!)
                        startActivity(intent)
                    }
                }
            }
            R.id.llRecharge -> {
                startActivity(Intent(this, RechargeActivity::class.java))
            }
            R.id.llTransfers -> {
                //showToast("转账")
                if (LOGIN_STATUS) {
                    if (IS_ACTIVATION == 1) {
                        ToastUtils.show(this, getString(R.string.account_no_activation))
                    } else {
                        if (assetsBean != null && !TextUtils.isEmpty(assetsBean?.name!!)) {
                            var intent = Intent(this, NewTransferActivity::class.java)
                            intent.putExtra("symbol", assetsBean?.name!!)
                            intent.putExtra("assetGuid", assetsBean?.assetGuid!!)
                            intent.putExtra("assetBalance", assetsBean?.amount!!)
                            startActivity(intent)
                        }
                    }
                } else {
                    EntranceActivity.start(this)
                }
            }
            R.id.llConvert -> {
                //showToast("兑换")
                if (LOGIN_STATUS) {
                    if (IS_ACTIVATION == 1) {
                        ToastUtils.show(this, getString(R.string.account_no_activation))
                    } else {
                        if (assetsBean != null && !TextUtils.isEmpty(assetsBean?.name!!)) {
                            var intent = Intent(this, ConvertActivity::class.java)
                            intent.putExtra("symbol", assetsBean?.name!!)
                            intent.putExtra("assetGuid", assetsBean?.assetGuid!!)
                            startActivity(intent)
                        }
                    }
                } else {
                    EntranceActivity.start(this)
                }
            }

        }
    }

    override fun onResume() {
        super.onResume()
        NetDataManager.getUserAssets()
    }
}